- drawing countour plots in Xi -
Contour plots are one way to visualize a 2-dimensional function. Say we want to show the contour lines of the function 'sin(x)*cos(y)'. First we have to subdivide the plane into small patches where Xi can calculate the above function. For this purpose tthe interval2d library function is implemented in Xi
( 1)>[x,y]=interval2d(-5,-5,5,5,30,30);Now x and y are 31x31 arrays filled with the x- and y-coordinates of points that subdivide the square (-5,-5) to (5,5). Now we are able to calculate the z-values of the above function
( 2)>z=sin(x)*cos(y);and plot the contour lines:
( 3)>contour(z);If the x- and y-coordinates are not given, the 'contour' function simply takes the indices of the z-array as coordinates. In this case the contour lines are drawn in the rectangle from (0,0) to (30,30). We can improve this: first erase the plot
( 3)>plot(\erase);and redraw the contour lines with given x- and y- coordinates:
( 4)>contour(z,x,y);Looks better. But you possibly do not agree with the actual choice of the levels. By default 'contour' scans for the minima and maxima of the z-array and devides this interval into 10 parts. Let's see if we can make a finer partitioning, but first erase the plot a second time:
( 5)>plot(\erase); ( 6)>contour(z,x,y,\nlevels=20);With the nlevels argument you explicitly set the number of levels contour should use. The contour lines around the maxima and minima of the function are a little rough - we can smooth them with the /curve argument:
( 7)>plot(\erase); ( 8)>contour(z,x,y,\nlevels=20,\curve);Be warned this can take some time. Xi has to calculate a cubic spline for each contour line.
If you don't want a linear spacing of the levels you can explicitly tell 'contour' at which z-values you want a contour line to be drawn:
( 9)>plot(\erase); ( 10)>contour(z,x,y,\levels={-0.6,-0.4,0.4,0.6});With this command at -0.6,-0.4,0.4 and 0.6 a contour line will be drawn. Also these lines can be smoothed by splines:
( 11)>plot(\erase); ( 12)>contour(z,x,y,\levels={-0.6,-0.4,0.4,0.6},\curve);With lables and nlables you optionally put some lables on the contour lines. If you set nlables exacly the number of lables containing the z-Values will be plotted. If you set lables you may plot arbitrary lables.
( 12)>contour(z,x,y,\labels={"-0.6","-0.4","0","0.4","0.6"}, \levels={-0.6,-0.4,0, 0.4,0.6},\curve); ( 13)>window(0,\clearAll); ( 13)>contour(z,x,y,\nlabels=10);Okay that's it.
Let's have some fun and try out a nice function that has played an important part in Bodo's diploma thesis (whereas Bodo's diploma thesis did not play an important part at all as the other authors think ;-).
( 13)>plot(\erase); ( 14)>[x,y]=interval2d(-10,-10,10,10,100,100); ( 15)>z=0; ( 16)>for(i=0;i<6;i++) z+=cos(x*sin(i*~pi/3)+y*cos(i*~pi/3)); ( 17)>contour(z,x,y,\levels={-2.75,-2.5,-2.25,-2,-1,0,1,2,3,4,5});If your data points are not definied on a grid (scattered data points) use the spline2d function to generate gridded data points.